using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Collections.Generic;

namespace System.Web.UI
{
    /// <summary>
    /// using RijndaelManaged Algorithm
    /// </summary>
    [Serializable]
    internal class RSAHelper 
    {
        public static void GetRsaKeys(out string XmlPublicKey, out string XmlPrivateKey)
        {
            using (RSACryptoServiceProvider Rsa = new RSACryptoServiceProvider(1024))
            {
                XmlPublicKey = Rsa.ToXmlString(false);
                XmlPrivateKey = Rsa.ToXmlString(true);
            }
        }


        /// <summary>
        /// Encrypt bytes into bytes using a rsa key 
        /// </summary>
        public static byte[] EncryptWithRsaKey(byte[] clearbytes, string xmlkey)
        {
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                rsa.FromXmlString(xmlkey);
                return rsa.Encrypt(clearbytes, true);
            }
        }



        /// <summary>
        /// Encrypt a string into a string using a rsa key 
        /// </summary>
        public static string EncryptWithRsaKey(string clearText, string xmlkey)
        {
            byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
            byte[] encryptedData = EncryptWithRsaKey(clearBytes, xmlkey);
            return Convert.ToBase64String(encryptedData);
        }


        /// <summary>
        /// decrypt bytes into bytes using a rsa key 
        /// </summary>
        public static byte[] DecryptWithRsaKey(byte[] clearbytes, string xmlkey)
        {
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                rsa.FromXmlString(xmlkey);
                return rsa.Decrypt(clearbytes, true);
            }
        }



        /// <summary>
        /// decrypt a string into a string using a rsa key 
        /// </summary>
        public static string DecryptWithRsaKey(string clearText, string xmlkey)
        {
            byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
            byte[] DecryptedData = DecryptWithRsaKey(clearBytes, xmlkey);
            return Encoding.Unicode.GetString(DecryptedData);
        }

        /// <summary>
        /// hash a byte into a byte  
        /// </summary>
        public static byte[] Hash(byte[] data)
        {
            SHA512Managed sha2 = new SHA512Managed();
            return sha2.ComputeHash(data);
        }

        /// <summary>
        /// hash a string into a string  
        /// </summary>
        public static string Hash(string text)
        {
            SHA512Managed sha2 = new SHA512Managed();
            byte[] encryptedBytes = Encoding.UTF8.GetBytes(text);
            return Convert.ToBase64String(sha2.ComputeHash(encryptedBytes));
        }

        /// <summary>
        /// digital sign a byte into a byte using private key  
        /// </summary>
        public static byte[] DigitalSign(byte[] data,string privateXmlKey)
        {
            byte[] hashedData = RSAHelper.Hash(data);
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(privateXmlKey);
            return rsa.SignHash(hashedData, CryptoConfig.MapNameToOID("SHA512"));
        }

        /// <summary>
        /// digital sign a string into a string using private key  
        /// </summary>
        public static string DigitalSign(string text, string privateXmlKey)
        {
            byte[] data = Encoding.UTF8.GetBytes(text);
            return Convert.ToBase64String(RSAHelper.DigitalSign(data, privateXmlKey));
        }


        /// <summary>
        /// VerifySign a byte using public key  
        /// </summary>
        public static bool VerifySign(byte [] data,byte[] signture, string publicXmlKey)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(publicXmlKey);
            return rsa.VerifyData(data, CryptoConfig.MapNameToOID("SHA512"), signture);
        }

        /// <summary>
        /// VerifySign a string using public key  
        /// </summary>
        public static bool VerifySign(string text, string signture, string publicXmlKey)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(publicXmlKey);
            return rsa.VerifyData(Encoding.UTF8.GetBytes(text), CryptoConfig.MapNameToOID("SHA512"), Convert.FromBase64String(signture));
        }


    }
}